package com.stylefeng.guns.modular.apple_id_manage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.CmdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.apple_id_manage.model.AppleIds;
import com.stylefeng.guns.modular.apple_id_manage.service.IAppleIdsService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 苹果账号管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-20 14:44:39
 */
@Controller
@RequestMapping("/appleIds")
public class AppleIdsController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(AppleIdsController.class);

    private String PREFIX = "/apple_id_manage/appleIds/";

    @Autowired
    private IAppleIdsService appleIdsService;

    /**
     * 跳转到苹果账号管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "appleIds.html";
    }

    /**
     * 跳转到添加苹果账号管理
     */
    @RequestMapping("/appleIds_add")
    public String appleIdsAdd() {
        return PREFIX + "appleIds_add.html";
    }

    /**
     * 跳转到修改苹果账号管理
     */
    @RequestMapping("/appleIds_update/{appleIdsId}")
    public String appleIdsUpdate(@PathVariable Integer appleIdsId, Model model) {
        AppleIds appleIds = appleIdsService.selectById(appleIdsId);
        model.addAttribute("item",appleIds);
        LogObjectHolder.me().set(appleIds);
        return PREFIX + "appleIds_edit.html";
    }

    /**
     * 获取苹果账号管理列表
     * @param page
     * @param limit
     * @param appleType 是否已使用，1表示查询已使用的账号列表
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",defaultValue = "20") Integer limit,
                       @RequestParam(value = "appleType",defaultValue = "0") Integer appleType) {
        return appleIdsService.appleIdList(appleType,page,limit);
    }

    @RequestMapping("/appleIds_import_excel")
    public String appleIdsImport() {
        return PREFIX + "appleIds_import_excel.html";
    }

    @RequestMapping(value = "/import/appleIds")
    @ResponseBody
    public Object importAppleID(@RequestParam("file") MultipartFile file) {
        appleIdsService.importExcelOfAppleID(file);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/changeStatus")
    @ResponseBody
    public Object changeStatus(@RequestParam("appId") Integer appId,
                               @RequestParam("status") Integer status) {
        appleIdsService.changeStatus(appId,status);
        return SUCCESS_TIP;
    }

    /**
     * 导出报表
     * @param type
     * @param month
     * @return
     */
    @RequestMapping(value = "/downloadExcel")
    @ResponseBody
    public void downloadExcel(@RequestParam("type") Integer type,
                               @RequestParam("month") String month,
                              HttpServletResponse response) {
        appleIdsService.exportExcel(response,type,month);
    }

}
